//
//  RegisiterViewController.m
//  Youhuiju
//
//  Created by liuovyue on 16/9/8.
//  Copyright © 2016年 yutenys. All rights reserved.
//

#import "RegisiterViewController.h"
#import "RegisterPasswordViewController.h"
#import "YORequestHander.h"


@interface RegisiterViewController ()
@property (nonatomic, strong)UILabel *labelTitle;
@property (nonatomic, strong)UIButton *buttonReturn;
@property (nonatomic, strong)UIView *viewBar;
@property (nonatomic, strong)UIButton *buttonSure;
@property (nonatomic, strong)UIView *viewBack;


@property (strong, nonatomic) IBOutlet NSLayoutConstraint *buttonWidth;

@property (strong, nonatomic) IBOutlet UIButton *buttonNext;

@property (strong, nonatomic) IBOutlet UILabel *labelTwo;

@property (strong, nonatomic) IBOutlet UILabel *labelThree;


@property (strong, nonatomic) IBOutlet UIButton *buttonVerifcation;

@property (nonatomic, strong)YORequestHander *requestHandler;

@property (strong, nonatomic) IBOutlet UITextField *textFieldPho;
@property (strong, nonatomic) IBOutlet UITextField *textFieldVertifaction;

@end

@implementation RegisiterViewController






- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    [self getData];
    [self createNavigationBar];
    self.buttonWidth.constant = 341*FitW;
    self.buttonNext.backgroundColor = [UIColor colorWithHexString:@"#fd487f"];
    
    self.labelTwo.font = [UIFont systemFontOfSize:12*FitW];
    self.labelThree.font = [UIFont systemFontOfSize:12*FitW];
    
    self.labelTwo.textColor = [UIColor colorWithHexString:@"#fd487f"];
    self.labelThree.textColor = [UIColor colorWithHexString:@"#fd487f"];
    
    [self.buttonVerifcation setTitleColor:[UIColor colorWithHexString:@"fd487f"] forState:UIControlStateNormal];

    [self.buttonVerifcation addTarget:self action:@selector(verfifactionAction) forControlEvents:UIControlEventTouchUpInside];
}

#pragma mark - 获取数据
- (YORequestHander *)requestHandler
{
    //    WeakObj(self);
    if (!_requestHandler) {
        _requestHandler = [YORequestHander shareInstance];
        //        _requestHandler.netWorkNotReachableHandler = ^(){
        //            DLog(@"当前网络不可用");
        //            [weakself.noNetworkView show];
        //        };
    }
    return _requestHandler;
}
-(void)getData
{
    
   
    NSLog(@"bbbbbbbb %@",self.textFieldPho.text);
//    NSDictionary *param = @{@"cusMobile":@"17741169094"};
    NSDictionary *param = @{@"cusId":@"lll",
                            @"cusPass":@"1234",
                            @"cusMobile":@"17741169094"};
    
                          
   
    
    [self.requestHandler getWithAPIName:RegisterAPI apiVersion:nil parameters:param cachIdentifier:nil success:^(id response) {
        DLog(@"请求成功~~~~~~~~~~~~response:%@",response);
        
      
       
        
    } failure:^(NSString *errorMessage) {
        DLog(@"请求失败~~~~~~~~~~~~error:%@",errorMessage);
       
    }];
    
    
   
    
}



#pragma mark - 验证码
- (void)verfifactionAction {
    [self getData];
    __block NSInteger time = 59;
    dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
    dispatch_source_t _time = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0, queue);
    dispatch_source_set_timer(_time, dispatch_walltime(NULL, 0), 1.0*NSEC_PER_SEC, 0);
    self.buttonVerifcation.userInteractionEnabled = NO;
    dispatch_source_set_event_handler(_time, ^{
        if (time <= 0) {
            dispatch_source_cancel(_time);
            dispatch_async(dispatch_get_main_queue()
                           , ^{
                               
                           
            [self.buttonVerifcation setTitle:@"重新发送" forState:UIControlStateNormal];
          
            [self.buttonVerifcation setTitleColor:[UIColor colorWithHexString:@"fd487f"] forState:UIControlStateNormal];
            
            self.buttonVerifcation.userInteractionEnabled = YES;
                           });
        }else {
            
            int seconds = time % 60;
            dispatch_async(dispatch_get_main_queue(), ^{
                
                
                [self.buttonVerifcation setTitle:[NSString stringWithFormat:@"%.2d",seconds] forState:UIControlStateNormal];
                self.buttonVerifcation.tintColor = [UIColor blackColor];
                [self.buttonVerifcation setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
                
                
            });
            time--;
            
            
            
            
        }
        
        
        
        
    });
    dispatch_resume(_time);

    
}




- (void)createNavigationBar{
    
    self.viewBar = [[UIView alloc]initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, 64)];
    self.viewBar.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:self.viewBar];
    
    
    
    
    
    
    self.labelTitle = [[UILabel alloc]init];
    self.labelTitle.text = @"验证手机号";
    self.labelTitle.textAlignment = NSTextAlignmentCenter;
    self.labelTitle.textColor = [UIColor blackColor];
    self.labelTitle.font = [UIFont systemFontOfSize:18*FitW];
    [self.viewBar addSubview:self.labelTitle];
    
    [_labelTitle mas_makeConstraints:^(MASConstraintMaker *make) {
        
        //make.top.equalTo(self.viewBar.mas_top);
        make.left.equalTo(self.viewBar.mas_left).offset(100*FitW);
        make.right.equalTo(self.viewBar.mas_right).offset(-100*FitW);
        make.bottom.equalTo(self.viewBar.mas_bottom).offset(-14*FitH);
        
        
        
    }];
    //返回按钮
    self.buttonReturn = [UIButton buttonWithType:UIButtonTypeCustom];
    [self.buttonReturn setImage:[UIImage imageNamed:@"zfanhuijian"] forState:UIControlStateNormal];
    [self.buttonReturn addTarget:self action:@selector(tuihuiAcion:) forControlEvents:UIControlEventTouchUpInside];
    [self.viewBar addSubview:self.buttonReturn];
    
    [_buttonReturn mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.left.equalTo(self.viewBar.mas_left).offset(17*FitW);
        make.width.mas_equalTo(22*FitW);
        make.height.mas_equalTo(22*FitH);
        make.bottom.equalTo(self.viewBar.mas_bottom).offset(-13*FitH);
        
        
        
    }];
    
   
    
    
}

- (void)tuihuiAcion:(UIButton *)button {
    [self.navigationController popViewControllerAnimated:YES];

}

- (IBAction)nextAction:(id)sender {
   
    [[UIApplication sharedApplication]sendAction:@selector(resignFirstResponder) to:nil from:nil forEvent:nil];
    
    RegisterPasswordViewController *password = [[RegisterPasswordViewController alloc]init];
    [self.navigationController pushViewController:password animated:YES];


}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
